package by.gravity.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Layout.Alignment;
import android.text.style.AlignmentSpan;

public class StringUtil {

	private static final String UTF_8 = "UTF-8";

	private static DecimalFormat decimalFormat = null;

	private static final String NUMBER_THOUSAND_PATTERN = "\\B(?=(?:\\d{3})+(?!\\d))";

	private static final String ZERO_STRING = "0";

	private static final String COLON_STRING = ":";

	public static boolean isEmpty(String string) {

		if (string == null || string.length() == 0) {
			return true;
		}

		return false;
	}

	public static String decodeString(String text) {

		if (isEmpty(text)) {
			return text;
		}

		try {
			return URLDecoder.decode(text, UTF_8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return text;
	}

	private static DecimalFormat getDecimalFormatInstance() {

		if (decimalFormat == null) {
			decimalFormat = new DecimalFormat();
			DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
			otherSymbols.setDecimalSeparator('.');
			otherSymbols.setGroupingSeparator(' ');
			decimalFormat.setMaximumFractionDigits(10);
			decimalFormat.setMinimumFractionDigits(0);

			decimalFormat.setDecimalFormatSymbols(otherSymbols);
		}
		return decimalFormat;
	}

	public static String getFriendltyTime(int hourOfDay, int minutes) {

		StringBuilder builder = new StringBuilder();
		builder.append(hourOfDay < 10 ? ZERO_STRING + hourOfDay : hourOfDay);
		builder.append(COLON_STRING);
		builder.append(minutes < 10 ? ZERO_STRING + minutes : minutes);
		return builder.toString();
	}

	public static String convertNumberToHumanFriednly(double value) {

		DecimalFormat decimalFormat = getDecimalFormatInstance();
		return decimalFormat.format(value);
	}

	public static String convertNumberToHumanFriednly(String number) {

		if (isEmpty(number)) {
			return null;
		}
		return number.replaceAll(NUMBER_THOUSAND_PATTERN, " ");

	}

	public static String uppercaseFirstLetter(String text) {

		return Character.toUpperCase(text.charAt(0)) + text.substring(1);
	}

	public static SpannableStringBuilder alignToRight(String text) {

		SpannableStringBuilder builder = new SpannableStringBuilder(text);
		builder.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		return builder;

	}

	public static String strJoin(String[] aArr, String sSep) {

		return strJoin(aArr, sSep, false);
	}

	public static String strJoin(String[] aArr, String sSep, boolean addQuotes) {

		StringBuilder sbStr = new StringBuilder();
		for (int i = 0, il = aArr.length; i < il; i++) {
			if (i > 0) {
				sbStr.append(sSep);
			}
			if (addQuotes) {
				sbStr.append("\"");
			}
			sbStr.append(aArr[i]);
			if (addQuotes) {
				sbStr.append("\"");
			}
		}
		return sbStr.toString();
	}
}
